package com.by.service.impl;

import com.by.bean.Employee;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.ex.BusinessException;
import com.by.common.utils.Assert;
import com.by.mapper.EmployeeMapper;
import com.by.mapper.UserMapper;
import com.by.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee, EmployeeMapper> implements EmployeeService {

    @Autowired
    UserMapper userMapper;

    /**
     * 如果方法中出现异常  事务会回滚
     *
     * @param entity 要保存的对象
     * @return
     */
    @Transactional  //事务管理
    @Override
    public int save(Employee entity) {
        //保存用户
        entity.getUser().setType(1);
        int rs = userMapper.save(entity.getUser());
        Assert.isNotSuccess(rs, "用户保存失败");
        //用户保存成功
        //保存员工
        return super.save(entity);
    }
}
